/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FormBarang.java
 *
 * Created on Jan 30, 2011, 2:14:50 PM
 */

package juwita;

import config.DBConnection;
import java.sql.*;
//import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JOptionPane;
import javax.swing.table.TableColumn;


/**
 *
 * @author Abah
 */
public class FormBarang extends javax.swing.JInternalFrame {
    private DefaultTableModel DftTabMode;
    private boolean isGolLoaded = false;
    private boolean isJenisLoaded = false;
    //private String urlValue="";
    java.sql.Connection conn = new DBConnection().connect();
    /** Creates new form FormBarang */
    public FormBarang() {
        initComponents();
        //this.sei(new ImageIcon(getClass().getResource("/resource/3D.png")).getImage());
        //this.setFrameIcon(new ImageIcon(getClass().getResource("/resource/3D.png").getImage()));

        tbBrg.getTableHeader().setReorderingAllowed(false);

        //ButtonGroup group = new ButtonGroup();
        //group.add(rbKode);
       /// group.add(rbNamaProduk);

        loadDataGolongan();
        loadDataJenis();
        btnEdit.setEnabled(false);
        btnDelete.setEnabled(false);
        tampilData();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnTengah = new javax.swing.JPanel();
        spBrg = new javax.swing.JScrollPane();
        tbBrg = new javax.swing.JTable();
        pnAtas = new javax.swing.JPanel();
        btnDelete = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();
        txtNamaGol = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtKdBRG = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        btnSearch = new javax.swing.JButton();
        txtSearchBrg = new javax.swing.JTextField();
        btnNew = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        txtNamaJenis = new javax.swing.JTextField();
        cbGol = new javax.swing.JComboBox();
        cbSearchBrg = new javax.swing.JComboBox();
        btnShowAll = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        cbJenis = new javax.swing.JComboBox();
        txtNamaBrg = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        cbProduk = new javax.swing.JComboBox();
        txtHargaBeli = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        txtJBks = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        txtJSlop = new javax.swing.JTextField();
        txtJBal = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        txtHJStokis = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        txtHJKanvas = new javax.swing.JTextField();
        txtHJGrosir = new javax.swing.JTextField();
        txtNamaProduk = new javax.swing.JTextField();

        setBackground(new java.awt.Color(255, 255, 255));
        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Data Barang");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/barang.png"))); // NOI18N
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameClosing(evt);
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
            }
        });

        pnTengah.setBackground(new java.awt.Color(255, 255, 255));
        pnTengah.setLayout(new java.awt.BorderLayout());

        tbBrg.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbBrg.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        tbBrg.setGridColor(new java.awt.Color(0, 0, 255));
        tbBrg.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                tbBrgMousePressed(evt);
            }
        });
        tbBrg.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tbBrgKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tbBrgKeyReleased(evt);
            }
        });
        spBrg.setViewportView(tbBrg);

        pnTengah.add(spBrg, java.awt.BorderLayout.CENTER);

        pnAtas.setBackground(new java.awt.Color(255, 255, 255));
        pnAtas.setPreferredSize(new java.awt.Dimension(410, 300));
        pnAtas.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        btnDelete.setText("Delete");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });
        pnAtas.add(btnDelete, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 220, 70, -1));

        btnEdit.setText("Edit");
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });
        pnAtas.add(btnEdit, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 220, 70, -1));
        pnAtas.add(txtNamaGol, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 10, 230, -1));

        jLabel2.setText("Nama Barang");
        pnAtas.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 100, -1, -1));

        txtKdBRG.setEditable(false);
        pnAtas.add(txtKdBRG, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 80, 110, -1));

        jLabel3.setText("Produk");
        pnAtas.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 50, -1, -1));

        btnSearch.setText("Search");
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });
        pnAtas.add(btnSearch, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 270, -1, -1));
        pnAtas.add(txtSearchBrg, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 250, 260, -1));

        btnNew.setText("New");
        btnNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNewActionPerformed(evt);
            }
        });
        pnAtas.add(btnNew, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 220, 70, -1));

        jLabel6.setText("Kode Barang");
        pnAtas.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 80, -1, -1));
        pnAtas.add(txtNamaJenis, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 30, 230, -1));

        cbGol.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "MCK" }));
        cbGol.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbGolItemStateChanged(evt);
            }
        });
        pnAtas.add(cbGol, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 10, 110, -1));

        cbSearchBrg.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Kode Barang", "Nama Barang" }));
        pnAtas.add(cbSearchBrg, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 230, 110, -1));

        btnShowAll.setText("Show All");
        btnShowAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnShowAllActionPerformed(evt);
            }
        });
        pnAtas.add(btnShowAll, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 270, 80, -1));

        jLabel5.setText("Cari :");
        pnAtas.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 210, -1, -1));

        cbJenis.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "MCK.002" }));
        cbJenis.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbJenisItemStateChanged(evt);
            }
        });
        cbJenis.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                cbJenisFocusGained(evt);
            }
        });
        pnAtas.add(cbJenis, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 30, 110, -1));
        pnAtas.add(txtNamaBrg, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 100, 360, -1));

        jLabel7.setText("Golongan");
        pnAtas.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, -1, -1));

        jLabel8.setText("Harga Beli");
        pnAtas.add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 120, -1, -1));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/p_barang.png"))); // NOI18N
        pnAtas.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 240, -1, -1));

        jLabel4.setText("Jenis");
        pnAtas.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 30, -1, -1));

        cbProduk.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbProdukItemStateChanged(evt);
            }
        });
        pnAtas.add(cbProduk, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 50, 110, -1));

        txtHargaBeli.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtHargaBeli.setText("0");
        pnAtas.add(txtHargaBeli, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 120, 110, -1));

        jLabel13.setText("slop");
        pnAtas.add(jLabel13, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 170, -1, -1));

        txtJBks.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtJBks.setText("0");
        pnAtas.add(txtJBks, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 190, 40, -1));

        jLabel14.setText("bks");
        pnAtas.add(jLabel14, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 190, -1, -1));

        txtJSlop.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtJSlop.setText("0");
        pnAtas.add(txtJSlop, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 170, 40, -1));

        txtJBal.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtJBal.setText("0");
        pnAtas.add(txtJBal, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 150, 40, -1));

        jLabel9.setText("bal");
        pnAtas.add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 150, -1, -1));

        jLabel15.setText("Jumlah");
        pnAtas.add(jLabel15, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 150, -1, -1));

        txtHJStokis.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtHJStokis.setText("0");
        pnAtas.add(txtHJStokis, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 170, 100, -1));

        jLabel10.setText("Harga Stokis");
        pnAtas.add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 170, -1, -1));

        jLabel11.setText("Harga Kanvas");
        pnAtas.add(jLabel11, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 130, -1, -1));

        jLabel12.setText("Harga Grosir");
        pnAtas.add(jLabel12, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 150, -1, -1));

        txtHJKanvas.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtHJKanvas.setText("0");
        pnAtas.add(txtHJKanvas, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 130, 100, -1));

        txtHJGrosir.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtHJGrosir.setText("0");
        pnAtas.add(txtHJGrosir, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 150, 100, -1));
        pnAtas.add(txtNamaProduk, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 50, 230, -1));

        pnTengah.add(pnAtas, java.awt.BorderLayout.PAGE_START);

        getContentPane().add(pnTengah, java.awt.BorderLayout.CENTER);

        setBounds(0, 0, 474, 505);
    }// </editor-fold>//GEN-END:initComponents

    private void formInternalFrameClosing(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameClosing
        // TODO add your handling code here:
        int clossing;
        clossing = JOptionPane.showConfirmDialog(this,
                "Tutup Data Barang?",
                "Konfirmasi",
                JOptionPane.YES_NO_OPTION);
        if (clossing == 0){
            this.dispose();
            FormMain.isfBrg = false;
        }
    }//GEN-LAST:event_formInternalFrameClosing

    private void tbBrgMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbBrgMousePressed
        getDataFromBrg();
}//GEN-LAST:event_tbBrgMousePressed

    private void tbBrgKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbBrgKeyPressed
        // TODO add your handling code here:
        getDataFromBrg();
}//GEN-LAST:event_tbBrgKeyPressed

    private void tbBrgKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbBrgKeyReleased
        // TODO add your handling code here:
        getDataFromBrg();
}//GEN-LAST:event_tbBrgKeyReleased

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        // TODO add your handling code here:
        if(btnDelete.getText().equals("Cancel")){
            kosongkan();
            tampilData();
        }else if(btnDelete.getText().equals("Delete")){
            deleteData();
        }
    }//GEN-LAST:event_btnDeleteActionPerformed

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
        // TODO add your handling code here:
        //BARU KOPAS
        if(btnEdit.getText().equals("Save")){
            if(txtNamaBrg.getText().equals("") ) {
                JOptionPane.showMessageDialog(this, "Nama Barang harus diisi!",
                        "Informasi",JOptionPane.INFORMATION_MESSAGE);
            }else{
                if(txtHargaBeli.getText().equals("0") ||
                   !validateNumber(txtHargaBeli.getText())){
                    JOptionPane.showMessageDialog(this, "Harga beli harus diisi!",
                        "Informasi",JOptionPane.INFORMATION_MESSAGE);
                }else{
                    if(validateNumber(txtJBal.getText()) &&
                       validateNumber(txtJSlop.getText()) &&
                       validateNumber(txtJBks.getText()) &&

                       validateNumber(txtHJKanvas.getText()) &&
                       validateNumber(txtHJGrosir.getText()) &&
                       validateNumber(txtHJStokis.getText()))
                                                            {//bukan nomor
                        if(Integer.parseInt(txtJBal.getText()) >=1 ||
                           Integer.parseInt(txtJSlop.getText()) >=1 ||
                           Integer.parseInt(txtJBks.getText()) >=1){
                            insertDataBrg();
                        }else{//semua 0
                            JOptionPane.showMessageDialog(this, "Data tidak valid, " +
                                "Bal, Slop, atau Bungkus harus diisi","Error",
                            JOptionPane.ERROR_MESSAGE);
                        }
                    }else{
                            JOptionPane.showMessageDialog(this, "Data tidak valid, " +
                            " ","Error",
                            JOptionPane.ERROR_MESSAGE);
                    }
                }
            }
        }else if (btnEdit.getText().equals("Edit")){
            editData();
        }

        //*
        //*/
}//GEN-LAST:event_btnEditActionPerformed

    private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
        // TODO add your handling code here:
        //BARU KOPAS

        tampilTabelSearch();
        txtKdBRG.setText("");
        txtNamaBrg.setText("");

        btnEdit.setEnabled(false);
        btnDelete.setEnabled(false);
}//GEN-LAST:event_btnSearchActionPerformed

    private void btnNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNewActionPerformed
        // TODO add your handling code here:

        kosongkan();
        btnEdit.setText("Save");
        btnDelete.setText("Cancel");
        btnEdit.setEnabled(true);
        btnDelete.setEnabled(true);

        //setDataGolongan();
        //setDataJenis();
        //setKodeBrg();
}//GEN-LAST:event_btnNewActionPerformed

    private void cbGolItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbGolItemStateChanged
        // TODO add your handling code here:
        if(isGolLoaded==true){            
            //cbJenis.removeAllItems();
            //cbProduk.removeAllItems();
            if(cbGol.getSelectedIndex() != 0){
                setDataGolongan();
            }else{
                txtNamaGol.setText(""); 
            }
            
            loadDataJenis();
            setDataJenis();
            
            loadDataProduk();
            setDataProduk();
                        
            setKodeBrg();
        }
}//GEN-LAST:event_cbGolItemStateChanged

    private void btnShowAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnShowAllActionPerformed
        // TODO add your handling code here:
        tampilData();
        txtSearchBrg.setText("");
}//GEN-LAST:event_btnShowAllActionPerformed

    private void cbJenisItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbJenisItemStateChanged
        // TODO add your handling code here:
        if(cbJenis.getItemCount()>0 && cbGol.getItemCount()>0){
            setDataJenis();

            loadDataProduk();
            setDataProduk();

            setKodeBrg();
            
        }else{
            System.out.println("data jenis kosong woi");
        }
}//GEN-LAST:event_cbJenisItemStateChanged

    private void cbJenisFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_cbJenisFocusGained
        // TODO add your handling code here:
}//GEN-LAST:event_cbJenisFocusGained

    private void cbProdukItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbProdukItemStateChanged
        // TODO add your handling code here:
        if(cbProduk.getItemCount()>0 &&
           cbJenis.getItemCount()>0 &&
           cbGol.getItemCount()>0){
            setDataProduk();
            setKodeBrg();
        }else{
            System.out.println("data produk kosong woi");
        }
    }//GEN-LAST:event_cbProdukItemStateChanged

    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnNew;
    private javax.swing.JButton btnSearch;
    private javax.swing.JButton btnShowAll;
    private javax.swing.JComboBox cbGol;
    private javax.swing.JComboBox cbJenis;
    private javax.swing.JComboBox cbProduk;
    private javax.swing.JComboBox cbSearchBrg;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel pnAtas;
    private javax.swing.JPanel pnTengah;
    private javax.swing.JScrollPane spBrg;
    private javax.swing.JTable tbBrg;
    private javax.swing.JTextField txtHJGrosir;
    private javax.swing.JTextField txtHJKanvas;
    private javax.swing.JTextField txtHJStokis;
    private javax.swing.JTextField txtHargaBeli;
    private javax.swing.JTextField txtJBal;
    private javax.swing.JTextField txtJBks;
    private javax.swing.JTextField txtJSlop;
    private javax.swing.JTextField txtKdBRG;
    private javax.swing.JTextField txtNamaBrg;
    private javax.swing.JTextField txtNamaGol;
    private javax.swing.JTextField txtNamaJenis;
    private javax.swing.JTextField txtNamaProduk;
    private javax.swing.JTextField txtSearchBrg;
    // End of variables declaration//GEN-END:variables


    private void kosongkan(){
        cbGol.setSelectedIndex(0);
        txtNamaBrg.setText("");

        txtHargaBeli.setText("0");
        txtHJKanvas.setText("0");
        txtHJGrosir.setText("0");
        txtHJStokis.setText("0");

        txtJBal.setText("0");
        txtJSlop.setText("0");
        txtJBks.setText("0");

        txtSearchBrg.setText("");
                
        btnEdit.setEnabled(false);
        btnDelete.setEnabled(false);
        btnEdit.setText("Edit");
        btnDelete.setText("Delete");

    }
    private void tampilData(){
        Object[] Baris={"No","Kode","Nama"};
        DftTabMode=new DefaultTableModel(null,Baris);
        tbBrg.setModel(DftTabMode);

        try {
            java.sql.Statement stmt = conn.createStatement();
            java.sql.ResultSet rslt = stmt.executeQuery("Select * from tbrg");
            while (rslt.next()) {
                Integer no = rslt.getRow();
                String sKdBarang = rslt.getString("kdBrg");
                String sNamaBarang = rslt.getString("nmBrg");
                //String sKet = rslt.getString("ket");
                //String sTest = rslt.getString("test");

                String[] dataField = {Integer.toString(no), sKdBarang, sNamaBarang};//, sKet};
                //DftTabMode.addRow(dataField);

                DftTabMode.addRow(dataField);
                int widths[] = {50, 110, 250};
                for(int i=0;i<tbBrg.getColumnCount();i++){
                    TableColumn column = tbBrg.getColumnModel().getColumn(i);
                    column.setPreferredWidth(widths[i]);
                }

            }

        }
        catch(SQLException e) {
            System.out.println("gagal : " + e.toString());
        }
    }

    private void tampilTabelSearch(){
        String srcby = "";
        if(cbSearchBrg.getSelectedItem().toString().equals("Kode Barang")){
            srcby = "kdBrg";
        }else{
            srcby = "nmBrg";
        }

        Object[] Baris={"No","Kode","Nama"};
        DftTabMode=new DefaultTableModel(null,Baris);
        tbBrg.setModel(DftTabMode);

        try {
            java.sql.Statement stmt = conn.createStatement();
            java.sql.ResultSet rslt = stmt.executeQuery(
                    "SELECT * FROM tbrg WHERE UPPER(" + srcby +
                    ") like UPPER('%" +
                    txtSearchBrg.getText() + "%')");
            //int iNo = 0;
            while (rslt.next()) {
                //iNo = iNo + 1;
                //iNo.
                Integer x = rslt.getRow();
                //
                String sKdBrg = rslt.getString("kdBrg");
                String sNamaBrg = rslt.getString("nmBrg");
                //String sNamaKota = rslt.getString("ket");

                String[] dataField = {Integer.toString(x), sKdBrg, sNamaBrg};
                DftTabMode.addRow(dataField);
                int widths[] = {50, 110, 250};
                for(int i=0;i<tbBrg.getColumnCount();i++){
                    TableColumn column = tbBrg.getColumnModel().getColumn(i);
                    column.setPreferredWidth(widths[i]);
                }
            }

        }
        catch(SQLException e) {
            System.out.println("gagal : " + e.toString());
        }
    }
    private void deleteData(){
            try {
                int deleting;
                deleting = JOptionPane.showConfirmDialog(this,
                        "Apakah Anda yakin akan menghapus "

                        //+ cbKodeKota.getSelectedItem().toString() +" "
                        +txtKdBRG.getText() +"?",
                        "Konfirmasi",
                        JOptionPane.YES_NO_OPTION);
                if (deleting == 0){

                    PreparedStatement pStatement = null;
                    String sql = "DELETE FROM tbrg " +
                            "WHERE kdBrg=? ";
                    pStatement = conn.prepareStatement(sql);

                    pStatement.setString(1, txtKdBRG.getText());

                    int intTambah = pStatement.executeUpdate();
                    if (intTambah >0){
                        JOptionPane.showMessageDialog(this,
                                "Delete sukses", "Informasi",
                                JOptionPane.INFORMATION_MESSAGE);

                    }else {
                        JOptionPane.showMessageDialog(this,
                                "Delete gagal", "Informasi",
                                JOptionPane.INFORMATION_MESSAGE);
                    }

                    pStatement.close();

                    kosongkan();
                    tampilData();
                }
            } catch(SQLException e){
                System.out.println("koneksi gagal " + e.toString());
            }
    }
    private void insertDataBrg(){

        java.sql.PreparedStatement pStatement = null;
        String sql = "INSERT INTO tbrg " +
                "(kdBrg, kdProduk, nmBrg, hrgBeli, hrgKanvas, hrgGrosir, " +
                "hrgStokis, bal, slop, bks, tgl_insert, tgl_update, user_id) " +
                "VALUES (?,?,?,?,?,?,?,?,?,?, now(),now(),?)";
        try {
            pStatement = conn.prepareStatement(sql);
            pStatement.setString(1, txtKdBRG.getText());
            String kdProduk = (String)cbProduk.getSelectedItem();
            pStatement.setString(2, kdProduk);
            pStatement.setString(3, txtNamaBrg.getText());
            pStatement.setDouble(4, Double.parseDouble(txtHargaBeli.getText()));
            pStatement.setDouble(5, Double.parseDouble(txtHJKanvas.getText()));
            pStatement.setDouble(6, Double.parseDouble(txtHJGrosir.getText()));
            pStatement.setDouble(7, Double.parseDouble(txtHJStokis.getText()));
            pStatement.setString(8, txtJBal.getText());
            pStatement.setString(9, txtJSlop.getText());
            pStatement.setString(10, txtJBks.getText());
            pStatement.setString(11, "admin");

            int intTambah = pStatement.executeUpdate();
            if (intTambah>0){
                JOptionPane.showMessageDialog(this,
                        "Penambahan data barang sukses", "Informasi",
                        JOptionPane.INFORMATION_MESSAGE);
            }else{
                JOptionPane.showMessageDialog(this,
                        "Penambahan data barang gagal", "Informasi",
                        JOptionPane.INFORMATION_MESSAGE);
            }
            pStatement.close();
            //conn.close();

            kosongkan();
            tampilData();

        }
        catch(SQLException e){
            System.out.println("koneksi gagal " + e.toString());
        }
    }

     private void editData(){
        try {
            PreparedStatement pStatement = null;
            String sql = "UPDATE tbrg " +
                    "SET nmBrg=?, " +
                    "hrgBeli=?, hrgKanvas=?, hrgGrosir=?, " +
                    "hrgStokis=?, bal=?, slop=?, bks=?, " +
                    "tgl_update=now() " +
                    "WHERE kdBrg=? ";
            pStatement = conn.prepareStatement(sql);

            pStatement.setString(1, txtNamaBrg.getText());
            pStatement.setDouble(2, Double.parseDouble(txtHargaBeli.getText()));
            pStatement.setDouble(3, Double.parseDouble(txtHJKanvas.getText()));
            pStatement.setDouble(4, Double.parseDouble(txtHJGrosir.getText()));
            pStatement.setDouble(5, Double.parseDouble(txtHJStokis.getText()));
            pStatement.setString(6, txtJBal.getText());
            pStatement.setString(7, txtJSlop.getText());
            pStatement.setString(8, txtJBks.getText());
            
            pStatement.setString(9, txtKdBRG.getText());

            int intTambah = pStatement.executeUpdate();
            if (intTambah >0){
                JOptionPane.showMessageDialog(this,
                        "Edit sukses", "Informasi",
                        JOptionPane.INFORMATION_MESSAGE);

            }else {
                JOptionPane.showMessageDialog(this,
                        "Edit gagal, Kode barang tidak bisa diubah", "Informasi",
                        JOptionPane.INFORMATION_MESSAGE);
            }

            //tbBarangGol.get
            pStatement.close();
            kosongkan();
            tampilData();


        } catch(SQLException e){
            System.out.println("koneksi gagal " + e.toString());
        }
    }

    private void getDataFromBrg(){
                // TODO add your handling code here:
        Object oKdBarang, oNamaBarang, oKdGol, oKdJenis, oKdProduk;//, oKet;//, oTest;

        oKdBarang = tbBrg.getValueAt(tbBrg.getSelectedRow(), 1);
        oNamaBarang = tbBrg.getValueAt(tbBrg.getSelectedRow(), 2);
        //oKet = tbBarangGol.getValueAt(tbBarangGol.getSelectedRow(), 2);
        //oTest = tbKota.getValueAt(tbKota.getSelectedRow(), 3);

        try {
            java.sql.Statement stmt = conn.createStatement();
            java.sql.ResultSet rslt = stmt.executeQuery("Select * from tbrg WHERE kdBrg=" +
                    "'" +oKdBarang+ "'");
            while (rslt.next()) {
                oKdGol = rslt.getString("kdProduk");
                cbGol.setSelectedItem(oKdGol.toString().substring(0, 3));

                oKdJenis = rslt.getString("kdProduk");
                //System.out.println("oi : " + oKdJenis.toString().substring(0,3));
                cbJenis.setSelectedItem(oKdJenis.toString().substring(0,7));

                oKdProduk = rslt.getString("kdProduk");
                cbProduk.setSelectedItem(oKdProduk);
                //txtKet.setText(rslt.getString("ket"));

                txtJBal.setText(rslt.getString("bal"));
                txtJSlop.setText(rslt.getString("slop"));
                txtJBks.setText(rslt.getString("bks"));
                
                txtHargaBeli.setText(rslt.getString("hrgBeli"));

                txtHJKanvas.setText(rslt.getString("hrgKanvas"));
                txtHJGrosir.setText(rslt.getString("hrgGrosir"));
                txtHJStokis.setText(rslt.getString("hrgStokis"));
                                
            }

        }
        catch(SQLException e) {
            System.out.println("gagal : " + e.toString());
        }

        /*
        if(oTest == null){
            oTest = " ";
        }
         *
         */

        txtKdBRG.setText(oKdBarang.toString());
        txtNamaBrg.setText(oNamaBarang.toString());

        btnEdit.setText("Edit");
        btnDelete.setText("Delete");
        btnEdit.setEnabled(true);
        btnDelete.setEnabled(true);
        setDataGolongan();
    }

    private void loadDataGolongan(){
        cbGol.removeAllItems();

        cbGol.addItem("- pilih gol -");
        try {
            java.sql.Statement stmt = conn.createStatement();
            java.sql.ResultSet rslt = stmt.executeQuery("Select * from tbrg_gol");
            while (rslt.next()) {
                Object data = (Object)rslt.getString("kdGol");
                cbGol.addItem(data);
            }

        }
        catch(SQLException e) {
            System.out.println("gagal : " + e.toString());
        }

        isGolLoaded = true;
        setDataGolongan();
    }
    private void loadDataJenis(){
        cbJenis.removeAllItems();
        //cbJenis.addItem("-- pilih golongan --");
        try {
            java.sql.Statement stmt = conn.createStatement();
            java.sql.ResultSet rslt = stmt.executeQuery("Select * from tbrg_jenis " +
                    "WHERE kdGol='" + cbGol.getSelectedItem().toString() + "'");
            while (rslt.next()) {
                Object data = (Object)rslt.getString("kdJenis");
                cbJenis.addItem(data);
            }

        }
        catch(SQLException e) {
            System.out.println("gagal : " + e.toString());
        }
        if (cbJenis.getItemCount() > 0){
            isJenisLoaded = true;
        }else{
            isJenisLoaded = false;
        }
        //setDataJenis();
    }

    private void loadDataProduk(){
        cbProduk.removeAllItems();
        if(cbJenis.getItemCount()>0){
            try {
                java.sql.Statement stmt = conn.createStatement();
                java.sql.ResultSet rslt = stmt.executeQuery("Select * from tbrg_produk " +
                        "WHERE kdJenis='" + cbJenis.getSelectedItem().toString() + "'");
                while (rslt.next()) {
                    Object data = (Object)rslt.getString("kdProduk");
                    cbProduk.addItem(data);
                }
            }
            catch(SQLException e) {
                System.out.println("gagal : " + e.toString());
            }
        }

        /*
        if (cbProduk.getItemCount() > 0){
            isJenisLoaded = true;
        }else{
            isJenisLoaded = false;
        }
         * 
         */
        //setDataJenis();
    }

    private void setDataGolongan(){
        
        try {
            java.sql.Statement stmt = conn.createStatement();
            java.sql.ResultSet rslt = stmt.executeQuery("Select * from tbrg_gol " +
                    "WHERE kdGol='" + cbGol.getSelectedItem().toString() + "'");
            while (rslt.next()) {
                txtNamaGol.setText(rslt.getString("nmGol"));
            }

        }
        catch(SQLException e) {
            System.out.println("gagal : " + e.toString());
        }
    }

    private void setDataJenis(){
        if(cbJenis.getItemCount() > 0 ){
            try {
                java.sql.Statement stmt = conn.createStatement();
                java.sql.ResultSet rslt = stmt.executeQuery("Select * from tbrg_jenis " +
                        "WHERE kdJenis='" + cbJenis.getSelectedItem().toString() + "'");
                while (rslt.next()) {
                    txtNamaJenis.setText(rslt.getString("nmJenis"));
                }

            }
            catch(SQLException e) {
                System.out.println("gagal : " + e.toString());
            }
        }else{
            System.out.println("Data jenis nya Kosong");
            txtNamaJenis.setText("");
        }
    }

    private void setDataProduk(){
        if(cbProduk.getItemCount() > 0 ){
            try {
                java.sql.Statement stmt = conn.createStatement();
                java.sql.ResultSet rslt = stmt.executeQuery("Select * from tbrg_produk " +
                        "WHERE kdProduk='" + cbProduk.getSelectedItem().toString() + "'");
                while (rslt.next()) {
                    txtNamaProduk.setText(rslt.getString("nmProduk"));
                }
            }
            catch(SQLException e) {
                System.out.println("gagal : " + e.toString());
            }
        }else{
            System.out.println("Data produk nya Kosong");
            txtNamaProduk.setText("");
        }
    }

    private void setKodeBrg(){
        if(cbProduk.getItemCount() > 0 ){
            try {
                String kdProduk = cbProduk.getSelectedItem().toString();
                java.sql.Statement stmt = conn.createStatement();
                java.sql.ResultSet rslt = stmt.executeQuery("Select * from tbrg " +
                        "WHERE kdProduk='" + kdProduk + "'");
                int x = 1;
                while (rslt.next()) {
                    x = x + 1;//rslt.getRow();
                    //System.out.println(x);
                }

                java.text.DecimalFormat nft = new java.text.DecimalFormat("#000.###");
                nft.setDecimalSeparatorAlwaysShown(false);
                txtKdBRG.setText(kdProduk + "." + nft.format(x));
                //System.out.println(txtKdJenis.getText());
                //cbJenis.setSelectedItem(kdGol.toString());

            }
            catch(SQLException e) {
                System.out.println("gagal : " + e.toString());
            }
        }else{
            txtKdBRG.setText(""); 
        }

    }

    public static boolean validateNumber(String num) {
        try {
            Integer.parseInt(num);
            return true;
        } catch (Exception e) {
            return false;
        }
       /*
     * if(validateNumber(num)){
            System.out.println("Correct Number");
        }else{
            System.out.println("Incorrect Number");
        }
     */
    }
}
